<template>
	<view class="page">
		<z-paging ref="paging" v-model="dataList" @query="queryList">
			<template #top>
				<u-navbar leftText="话题列表" safeAreaInsetTop fixed placeholder :autoBack="true"></u-navbar>
				<view style="padding: 20rpx 32rpx 32rpx 32rpx;background: #fff;">
					<view style="background: #f2f2f2;border-radius: 24rpx;">
						<view style="display: flex;align-items: center;">
							<!-- <image src="@/static/img/ss.png" mode="aspectFill" style="width: 40rpx;height: 40rpx;"></image> -->
							<!-- <view style=""> -->
								<!-- <input type="text" placeholder="请搜索" @ /> -->
								<u-search placeholder="请搜索" v-model="keyword" :show-action="false"
								@search="onSearch"></u-search>
								<!-- <u-search :showAction="true" actionText="请搜索" :animation="true"></u-search> -->
							<!-- </view> -->
						</view>
					</view>
				</view>
				<view style="padding: 24rpx;background: #fff;border-bottom: solid 1px #f2f2f2;">
					<u-tabs :list="tabList" :current="tabIndex" @change="changeTab" :is-scroll="false"></u-tabs>
				</view>
			</template>
			
			<!-- 列表 -->
			<view style="">
				<view style="padding: 24rpx 32rpx 0  32rpx;margin-bottom: 20rpx;" v-for="(item, index) in dataList" :key="index">
					<view style="border-bottom: #ccc solid 1px;padding-bottom: 24rpx;">
						
						<view class="flex" style="display: flex;margin-top: 16rpx;align-items: center;">
							<!-- <view class="">
								<image :src="item.image" mode="aspectFill" style="width: 50px;height: 50px;border-radius: 50%;
								margin-right: 20rpx;"></image>
							</view> -->
							<view class="c" style="flex: 1;font-size: 28rpx;padding-right: 24rpx;">
								<view style="font-weight: bold;">{{item.master_name}}</view>
								<view style="font-size: 24rpx;color: #999;margin-top: 10rpx;">
									<view>{{item.master_address}}</view>
								</view>
							</view>
							<view style="font-size: 28rpx;">
								<view v-if="tabIndex == 0">
									<view v-if="isTagSelected(item)" style="color: #fa3534;border: 2rpx solid #fa3534;
									border-radius: 60rpx;padding: 10rpx 0rpx;width: 70px;
									text-align: center;" @click="toggleTagSelect(item)">取消分配</view>
									<view v-else style="color: #1877f2;border: 2rpx solid #1877f2;
									border-radius: 60rpx;padding: 10rpx 0rpx;width: 70px;
									text-align: center;" @click="toggleTagSelect(item)">确定分配</view>
								</view>
								<view v-if="tabIndex == 0" style="color: #fa3534;border: 2rpx solid #fa3534;
								border-radius: 60rpx;padding: 10rpx 0rpx;width: 70px;margin-top: 12rpx;
								text-align: center;" @click="statusSelect(item, 2)">取消合作</view>
								
								<view v-if="tabIndex == 1" style="color: #1877f2;border: 2rpx solid #1877f2;
								border-radius: 60rpx;padding: 10rpx 0rpx;width: 70px;
								text-align: center;" @click="statusSelect(item, 1)">通过审核</view>
								<view v-if="tabIndex == 1" style="color: #fa3534;border: 2rpx solid #fa3534;
								border-radius: 60rpx;padding: 10rpx 0rpx;width: 70px;margin-top: 12rpx;
								text-align: center;" @click="statusSelect(item, 2)">不通过</view>
								
								<view v-if="tabIndex == 2" style="color: #1877f2;border: 2rpx solid #1877f2;
								border-radius: 60rpx;padding: 10rpx 0rpx;width: 70px;
								text-align: center;" @click="statusSelect(item, 1)">重新合作</view>
							</view>
						</view>
				
					</view>
				</view>
			</view>
			
			<template #bottom>
				<view class="footer">
					<button class="confirm-btn" @click="confirmSelection" style="background: #1877f2;color: #fff;">确定</button>
				</view>
			</template>
		</z-paging>
		
		<u-toast ref="uToast" />
		
		
		<view style="height: 200rpx;"></view>
		<!-- <view style="width: 100%;position: fixed;bottom: 0;">
			<view style="background: #fff;display: flex;align-items: center;justify-content: center;
			padding: 34rpx 34rpx 54rpx 34rpx;font-size: 32rpx;">
				<view style="color: #fff;background: #1877f2;
				padding: 20rpx 0;border-radius: 60rpx;width: 100%;text-align: center;"
				@click="onSubmitSave">+ 新增常用药品</view>
			</view>
		</view> -->
		
		<!-- <view style="position: fixed;right: 32rpx;bottom: 204rpx;" @click="jumpTo('/pages/prescription/ordinaryEdit')">
			<image src="@/static/img/add.png" mode="widthFix" style="width: 80rpx;"></image>
		</view> -->
		<!-- 更多操作 -->
		<u-action-sheet :list="moreList" v-model="moreShow" border-radius="32"
		@click="moreClick"></u-action-sheet>
		
		<!-- 删除弹窗 -->
		<u-modal v-model="statusShow" :title="statusTitle" :show-cancel-button="true" confirm-text="确认"
		:title-style="{fontWeight: 'bold'}" @confirm="statusYes">
			<view class="slot-content">
				<view style="padding: 48rpx 32rpx 64rpx 32rpx;">
					<view style="font-size: 28rpx;text-align: center;"
					:style="{color: statusType == 2 ? '#fa3534' : '#101010'}">{{statusContent}}</view>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import { request } from 'utils/request.js';
	export default {
		data() {
			return {
				statusId: 0,
				statusType: 0,
				statusShow: false,
				statusTitle: '温馨提醒',
				statusContent: '是否确定审核通过么？',
								
				dataList: [],
				keyword:'',
				tabIndex: 0,
				tabList: [
					{ name: '合作师傅' },
					{ name: '审核中' },
					{ name: '不合作' }
				],
				
				selectedTags: [], // 当前已选标签
				searchText: '', // 搜索文本
				filteredTags: [] ,// 过滤后的标签列表
				
				order_id: 0
			};
		},
		onLoad(options) {
		    // 从父页面传递的已选标签
		    if (options.selected) {
				this.selectedTags = JSON.parse(options.selected);
		    }
			if(options.order_id){
				this.order_id = options.order_id;
			}
		    // this.filteredTags = [...this.allTags];
		},
		methods: {
			// 审核
			statusSelect(item, status){
				this.statusType = status;
				this.statusId = item.id;
				if(status == 2){
					this.statusContent = '是否确定审核不通过么？';
				}
				this.statusShow = true;
			},
			statusYes(){
				// console.log(this.statusId)
				// console.log(this.statusType);
				request.getWithToken({
					url: '/master/getStatusLis',
					data: {id: this.statusId, statusType: this.statusType}
				}).then(res=>{
					console.log(res);
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none'
					});
					if(res.code == 1){
						this.$refs.paging.reload()//重置
					}
				})
			},
			
			changeTab(index) {
				this.tabIndex = index;
				this.$refs.paging.reload()//重置
			},
			// 搜索标签
			onSearch() {
				this.$refs.paging.reload()//重置
			},
			// 切换标签选择状态
			toggleTagSelect(tag) {
				const index = this.selectedTags.findIndex(t => t.id === tag.id);
				if (index >= 0) {
					// 已选中，取消选择
					this.selectedTags.splice(index, 1);
				} else {
					// 未选中，添加到已选
					this.selectedTags.push(tag);
				}
			},
			// 检查标签是否已选中
			isTagSelected(tag) {
				return this.selectedTags.some(t => t.id === tag.id);
			},
			// 确认选择
			confirmSelection() {
				console.log('this.selectedTags: ', this.selectedTags);
				request.postWithToken({
					url: '/order/postAssignMaster',
					data: {selectedTags : this.selectedTags, order_id: this.order_id}
				}).then(res=>{
					console.log(res);
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack();
						uni.$emit('htmlRefresh', {});
					}, 600);
				})
				return;
				// 保存到本地存储
				// uni.setStorageSync('selectedTags', JSON.stringify(this.selectedTags));
				let selectedTagsJson = JSON.stringify(this.selectedTags);
				// 返回上一页
				setTimeout(() => {
					uni.navigateBack();
					uni.$emit('htmlRefresh', {selectedTagsJson: selectedTagsJson});
				}, 600);
				// uni.navigateBack();
			},
			moreClick(e){
				console.log(e)
			},
			// 获取列表
			queryList(pageNo, pageSize) {
				
				let params = {};
				params.page = pageNo;
				params.keyword = this.keyword;
				params.tabIndex = this.tabIndex;
				request.getWithToken({
					url: '/master/getLis',
					data: params
				}).then(res=>{
					// console.log(res)
					// this.$refs.paging.complete(res.data);
					this.filteredTags = [...res.data.dataList];
					this.$refs.paging.complete(this.filteredTags);
				})
			},
			followClick(e){
				let talkData = uni.getStorageSync('talkData');
				
			},
			jumpTo(path){
				uni.navigateTo({
					url: path
				})
			},
			onSubmitSave(){
				uni.navigateTo({
					url: '/pages/prescription/selectMedicine'
				})
			}
		}
	}
</script>

<style>
	page{
		background-color: #f2f2f2 !important;
	}
</style>
<style lang="scss" scoped>
	
</style>
